class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(object):
    def findSecondMinimumValue(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        min_val = root.val
        self.flag = False
        self.second_min = 0

        def search(node):
            if node.left:
                if node.left.val > node.val:
                    if not self.flag:
                        self.second_min = node.left.val
                        self.flag = True
                    else:
                        self.second_min = min(self.second_min, node.left.val)
                    search(node.right)
                else:
                    if node.right.val > node.val:
                        if not self.flag:
                            self.second_min = node.right.val
                            self.flag = True
                        else:
                            self.second_min = min(self.second_min, node.right.val)
                        search(node.left)
                    else:
                        search(node.left)
                        search(node.right)
        search(root)
        if self.flag:
            return self.second_min
        else:
            return -1
